<template>
  <div class='container'>
    <el-card style="margin: 10px 15px">
      <div slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right" >
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{subject.name}}</el-breadcrumb-item>
          <el-breadcrumb-item>目录管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row>
        <!-- 表单 -->
        <el-col :span="18">
          <el-form :inline="true" size="small">
          <el-form-item style="margin-right: 50px" label="目录名称">
              <el-input v-model="requestParams.tagName"></el-input>
          </el-form-item>
            <el-form-item label="状态">
                 <el-select v-model="requestParams.state" placeholder="请选择">
                <el-option label="已启用" :value="1"></el-option>
                <el-option label="已禁用" :value="0"></el-option>
              </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click="clear()">清除</el-button>
            <el-button @click="files()" type="primary">搜索</el-button>
          </el-form-item>
        </el-form>
        </el-col>

        <!-- 按钮 -->
        <el-col :span="6">
         <el-button style="float:right" icon="el-icon-edit" @click="tagDialog()" type="success" size="small">新增目录</el-button>
         <el-button v-if="subject.id && subject.name" type="text" style="float:right" icon="el-icon-back" @click="$router.back()" size="small">返回学科</el-button>
        </el-col>
      </el-row>
      <el-alert :title="`数据一共${total}条`" type="info" :closable="false" style="margin-bottom: 15px" show-icon>
        </el-alert>
        <el-table :data="tags">
            <el-table-column width="90px" label="序号" type="index" />
            <el-table-column label="所属学科" prop="subjectName" />
            <el-table-column label="标签名称" prop="tagName" />
            <el-table-column label="创建者" prop="username" />
            <el-table-column label="创建日期" width="200px">
              <template slot-scope="scope">{{scope.row.addDate|parseTimeByString}}</template>
            </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="scope">{{scope.row.state===1?'已启用':'已禁用'}}</template>
            </el-table-column>
            <el-table-column label="操作" width="240px">
              <template slot-scope="scope">
                <el-button type="text" @click="toggleState(scope.row)">{{scope.row.state===1?'启用' : '禁用'}}</el-button>
                <el-button type="text" @click="tagDialog(scope.row)" :disabled="scope.row.state===0">修改</el-button>
                <el-button type="text" @click="deltag(scope.row)" :disabled="scope.row.state===0 || scope.row.total < 1">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
           <el-pagination
            :current-page="requestParams.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="requestParams.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            class="pager"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            >
          </el-pagination>
        <tags-add ref="tagadd" :data="currtag" @handleTag="handleTag()" />
    </el-card>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/tags.js'
import tagsAdd from '../components/tags-add.vue'
export default {
  components: { tagsAdd },
  name: 'tags-page',
  data () {
    return {
      showDialog: false,
      // 筛选参数
      requestParams: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      tags: [],
      total: 0,
      currtag: {}
    }
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  created () {
    this.gettags()
  },
  watch: {
    '$route.query': function () {
      this.gettags()
    }
  },
  methods: {
    // 删除
    async deltag (tag) {
      await this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        type: 'warning'
      })
      await remove(tag)
      this.$message.success('删除成功')
      this.gettags()
    },
    // 获取表格数据列表
    async gettags () {
      const { data } = await list(this.requestParams)
      this.tags = data.items
      this.total = data.counts
    },
    // 新增 修改
    handleTag () {
      if (!this.currtag.id) {
        this.requestParams.page = 1
      }
      this.gettags()
    },
    // 展开对话框
    tagDialog (tag = {}) {
      this.currtag = tag
      this.$nextTick(() => {
        this.$refs.tagadd.open()
      })
    },
    // 切换状态
    async toggleState (tag) {
      await changeState({
        id: tag.id,
        state: tag.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      tag.state = tag.state === 1 ? 0 : 1
    },
    // 清除
    clear () {
      this.requestParams = {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
      this.gettags()
    },
    // 搜索
    files () {
      this.requestParams.page = 1
      this.gettags()
    },

    // 切换条数
    handleSizeChange (size) {
      this.requestParams.page = 1
      this.requestParams.pagesize = size
      this.gettags()
    },
    // 切换分页
    handleCurrentChange (np) {
      this.requestParams.page = np
      this.gettags()
    }
  }
}
</script>

<style scoped lang='scss'>
.container {
  .pager {
    margin-top: 15px;
    margin-bottom: 15px;
    float: right;
  }
}
</style>
